package com.media.api;

import com.media.model.Comment;
import com.base.model.R;
import com.media.model.User;
import com.media.model.dto.CommentDto;
import com.media.service.CommentService;
import com.media.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.catalina.security.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpRequest;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ServerWebExchange;

import java.sql.Timestamp;
import java.util.*;

/**
 * @Author: Lenovo
 * @Description: TODO
 * @DateTime: 2023/9/14 15:16
 **/
@Api(tags = "评论接口")
@RestController
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private CommentService commentService;

    private static final String COMMENT_PRE = "comment_";

    private static final String USER_PRE = "user_";


    @ApiOperation("添加评论")
    @PostMapping("/mediaComment")
    @PreAuthorize("hasAuthority('commentMedia')")
    public R<String> mediaComment(@RequestBody Comment comment) {
        //1.从SpringSecurity中获取用户Id和用户姓名，填充至comment中
        User user = SecurityUtils.getUser();
        comment.setUserId(user.getId());
        comment.setUserName(user.getUsername());
        //2.填充评论时间，点赞数
        comment.setCommentTime(new Timestamp(System.currentTimeMillis()));
        comment.setLikesAmount(0);
        return commentService.mediaComment(comment);
    }


    @PostMapping("/listComment/{mediaId}")
    public R<List<CommentDto>> listComment(@PathVariable String mediaId) {
        return commentService.listComment(mediaId);
    }

    @PostMapping("/commentLike/{commentId}")
    @PreAuthorize("hasAuthority('commentLike')")
    public R<String> commentLike(@PathVariable Integer commentId) {
        //用一个Set来维护某个用户点赞的记录，将其存入redis中，使用用户Id作为Key，Map作为value
        //1.从SpringSecurity中获取用户Id
        User user = SecurityUtils.getUser();
        Integer userId=user.getId();
        if (redisTemplate.hasKey(USER_PRE + userId)) {
            Set set = (Set) redisTemplate.opsForValue().get(USER_PRE + userId);
            set.add(commentId);
            redisTemplate.opsForValue().set(USER_PRE + userId, set);
        } else {
            Set set = new HashSet();
            set.add(commentId);
            redisTemplate.opsForValue().set(USER_PRE + userId, set);
        }
        redisTemplate.opsForValue().increment("comment_" + commentId);
        return R.success("点赞成功");
    }

    @PostMapping("/commentLikeCancel/{commentId}")
    @PreAuthorize("hasAuthority('commentLikeCancel')")
    public R<String> commentLikeCancel(@PathVariable Integer commentId) {
        //1.从SpringSecurity中获取用户Id
        User user = SecurityUtils.getUser();
        Integer userId=user.getId();
        Set set = (Set) redisTemplate.opsForValue().get(USER_PRE + userId);
        set.remove(commentId);
        redisTemplate.opsForValue().set(userId,set);
        redisTemplate.opsForValue().decrement("comment_"+commentId);
        return R.success("取消点赞成功");
    }

}
